package com.example.sqlview;

import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import java.util.List;

public class MainActivity extends AppCompatActivity {
    private com.qgxy.mysqlite.MySQLiteOpenHelper openHelper;
    private Button btnInsert, btnDel, btnQuery, btnUpdate;
    private EditText etName, etNumber, etGender, etScore;
    private ListView lv;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);

        setContentView(R.layout.activity_main);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });
        initView();

        openHelper = new com.qgxy.mysqlite.MySQLiteOpenHelper(this);
        StudentBean student01 = new StudentBean();

        btnInsert.setOnClickListener(v -> {
            StudentBean studentBean = new StudentBean();
            String name = etName.getText().toString().trim();
            studentBean.setName(name);
            studentBean.setNumber(etNumber.getText().toString().trim());
            studentBean.setGender(etGender.getText().toString().trim());
            studentBean.setScore(etScore.getText().toString().trim());
            if (name.isEmpty()){
                Toast.makeText(this, "请输入名字", Toast.LENGTH_SHORT).show();
            }else {
                long l = openHelper.insertData(studentBean);
                if (l>0){
                    Toast.makeText(this, "写入成功", Toast.LENGTH_SHORT).show();
                }else {
                    Toast.makeText(this, "写入失败", Toast.LENGTH_SHORT).show();
                }
            }
        });

        btnDel.setOnClickListener(v -> {
            int i = openHelper.deleteDataFromDbByName(etName.getText().toString().trim());
            if (i >0){
                Toast.makeText(this, "删除成功", Toast.LENGTH_SHORT).show();
            }else{
                Toast.makeText(this, "删除失败", Toast.LENGTH_SHORT).show();
            }
        });

        btnUpdate.setOnClickListener(v -> {
            StudentBean student = new StudentBean();
            student.setName(etName.getText().toString().trim());
            student.setNumber(etNumber.getText().toString().trim());
            student.setGender(etGender.getText().toString().trim());
            student.setScore(etScore.getText().toString().trim());
            int i = openHelper.updateDataByName(student);
            if (i > 0){
                Toast.makeText(this, "更新成功", Toast.LENGTH_SHORT).show();
            }else {
                Toast.makeText(this, "更新失败", Toast.LENGTH_SHORT).show();
            }
        });

        btnQuery.setOnClickListener(v -> {
//            List<StudentBean> studentList = openHelper.queryAllData();
//            if (studentList != null && studentList.size() > 0) {
//                StudentAdapter adapter = new StudentAdapter(MainActivity.this, studentList);
//                lv.setAdapter(adapter);
//            } else {
//                Toast.makeText(MainActivity.this, "没有查询到数据", Toast.LENGTH_SHORT).show();
//            }
        });
    }

    private void initView() {
        etName = findViewById(R.id.et_name);
        etNumber = findViewById(R.id.et_number);
        etGender = findViewById(R.id.et_gender);
        etScore = findViewById(R.id.et_score);
        btnInsert = findViewById(R.id.btn_insert);
        btnDel = findViewById(R.id.btn_del);
        btnUpdate = findViewById(R.id.btn_update);
        btnQuery = findViewById(R.id.btn_query);
    }

}